 
var Swato = {
	Version: '1.0',
	Author:'Rain'
};


Swato.Engine = Class.create();

Swato.Engine.prototype = {

	initialize: function(options) {		
		this.url = 'RPCServlet.swato';
		this.conn_id = Math.floor(Math.random()*100000000);
		this.options = new Object();
		this.options.contentType = 'text/html;chartset=UTF-8';
		this.htmlContent = '<div id="processing-mask"  style=\"display: none;\">&#160;</div><div id=\"processing\" style=\"display: none;\"><div class=\"processing-indicator\"><img src=\"'+Global.contextPath+'/script/swato/processing.gif\" align=\"absmiddle\"/>请求处理中,请稍侯 ...</div></div>';
		Object.extend(this.options,options);
	},
	
	hide: function(){
			//////////////////////////////
			//隐藏请求处理进度条
			if($('processing')){$('processing').hide();}
			if($('processing-mask').visible()){$('processing-mask').hide();}	
			//////////////////////////////
	},

	call: function(serviceDotMethodName,params,catcher,options) {
		if ( this.url == null ) return;
		/////////////////////////////////////
		//显示请求处理进度条
		if($('processing') == undefined){
				new Insertion.Top(document.body,this.htmlContent);
			}
		$('processing-mask').show();
		$('processing').show();
		//////////////////////////////////////
		var serviceId=serviceDotMethodName.substring(0,serviceDotMethodName.lastIndexOf("."));
		var methodName=serviceDotMethodName.substring(serviceDotMethodName.lastIndexOf(".")+1,serviceDotMethodName.length);
		//处理参数
		if(params == undefined){
			params = [];
		}else if(!(params instanceof Array)){
			params = [params];
		}
		var url = this.url + "?serviceId="+serviceId;
		
		if (document.getElementById(catcher)!=null) {
			new Ajax.Updater(catcher, url,this._requestOptions(serviceId.hashCode||serviceId,methodName,params,options));
		} else if (catcher!=null){
			var option=new Object();
			Object.extend(option,options);
			if(typeof catcher == 'function'){
				var object = new Object();
				object.onSuccess = catcher;
				option.onComplete=this._onRequestComplete.bind(object);
			}else if(typeof catcher == 'object'){
				option.onComplete=this._onRequestComplete.bind(catcher);
			}			
			new Ajax.Request(url,this._requestOptions(serviceId.hashCode||serviceId,methodName,params,option));
		}else{
			var option = new Object();
			Object.extend(option,options);
			option.asynchronous = false;
			var object = new Object();
			var __response = '';
			object.onSuccess = function(result){__response = result;};
			option.onComplete=this._onRequestComplete.bind(object);			
			new Ajax.Request(url,this._requestOptions(serviceId.hashCode||serviceId,methodName,params,option));
			return __response;
		}
	},
	
	_requestOptions: function(remoteObjectID,methodName,params,options) {
		var sendingData= {
			"params":params,
			"method":methodName,
			"id":remoteObjectID,
			"conn_id":this.conn_id};
		var requestOptions=new Object();
		Object.extend(requestOptions,this.options);
		Object.extend(requestOptions,options);
		requestOptions.parameters=sendingData;
		//if (typeof(debug)== 'function') debug(">>> "+JSON.valueOf(requestOptions.parameters));
		requestOptions.__invoker = 'swato';
		return requestOptions;
	},

	_onRequestComplete : function(response) {
		if (response.status != 200) {
			if (typeof(FVL_log)== 'function') FVL_log("Response Status: "+response.status,FVL_ERROR);
			//////////////////////////////
			//隐藏请求处理进度条
			if($('processing')){$('processing').hide();}
			if($('processing-mask').visible()){$('processing-mask').hide();}	
			//////////////////////////////
			return;
		}
		//////////////////////////////
		//隐藏请求处理进度条
		if($('processing')){$('processing').hide();}
		if($('processing-mask').visible()){$('processing-mask').hide();}	
		//////////////////////////////
		if (typeof(debug)== 'function')	debug("<<< "+response.responseText);
		var root;
		var result;
		var error;
		//try parse XML
		root=XMLUtil.getDocumentElement(response);
		if (root){ 
			//deal with xml
			var d2j = new JKL.ParseXML.JSON();
			result = d2j.parseDocument( root );
			if (typeof(debug)== 'function') {
				debug("XML parse result: "+JSON.valueOf(result));
			}
		}else { 
			//deal with json
			var responseText;
			eval("responseText = " + response.responseText);
			result=responseText.result;
			error=responseText.error;
			this.hashCode=responseText.hashCode;
		}
		if (this.onError!=null && error!=undefined) {
			this.onError.apply(this, [error]);
		}
		//操作成功
		if (error == undefined){
			if(this.onSuccess!=null ){
				this.onSuccess.apply(this, [result]);
			}else if(this.gotResult!=null ){
				this.gotResult.apply(this, [result]);
			}
			
		}
	}
};

var XMLUtil = {
	getForIE: function(response){
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(response.responseText);
		return xmlDoc.documentElement;
	},

	getDocumentElement: function(response) {
		if (response.responseText!=null && response.responseText.charAt(0)=='{') return null;
		if (response.responseXML!=null && response.responseXML.documentElement!=null)
			return response.responseXML.documentElement;
		if (response.documentElement!=null) return response.documentElement;
		return (XMLUtil.getForIE(response));
	}
}
//对参数进行编码
function encodeParam(param){
		if(typeof param == "string"){
			return encodeURIComponent(param);
		}else if(typeof param == "object" || param instanceof Array){
			for(var p in param){
				param[p]= encodeParam(param[p]);
			}
		}
		return param;
	};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
//输出mask框的样式
document.write('<style type="text/css">');
document.write('#processing-mask{position:absolute;width:100%;height:100%;left:0;top:0;padding:2px;z-index:2000;background:#c3daf9;filter:alpha(opacity=40);}');
document.write('#processing{position:absolute;left:45%;top:40%;padding:2px;z-index:20001;height:auto;}');
document.write('#processing img {margin-bottom:0px;margin-right:8px;height: 30px;width: 30px;}');
document.write('#processing .processing-indicator{color:#6596cf;font:bold 13px tahoma,arial,helvetica;padding:10px;border:1px solid #a3bad9;background: #FFFFFF;}');
document.write('</style>');